package cn.fch.gotrip.controller;

import cn.fch.gotrip.api.BeanResponse;
import cn.fch.gotrip.entity.Hotel;
import cn.fch.gotrip.entity.Room;
import cn.fch.gotrip.exception.ValueException;
import cn.fch.gotrip.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-19 00:17
 **/
@RestController
@RequestMapping("hotel")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    @PostMapping
    public BeanResponse addHotel(@RequestBody Hotel hotel) {
        if (hotelService.addHotel(hotel)) {
            return BeanResponse.ok(hotel, "添加成功");
        } else {
            return BeanResponse.badRequest("添加失败");
        }
    }

    @PostMapping("room")
    public BeanResponse addHotelRoom(@RequestBody Room room) throws ValueException {
        if (hotelService.addHotelRoom(room)) {
            return BeanResponse.ok(room, "添加成功");
        } else {
            return BeanResponse.badRequest("添加失败");
        }
    }

    @PutMapping
    public BeanResponse updateHotel(@RequestBody Hotel hotel) {
        if (hotelService.updateHotel(hotel)) {
            return BeanResponse.ok(hotel, "修改成功");
        } else {
            return BeanResponse.badRequest("修改失败");
        }
    }

    @PutMapping("{id}/html")
    public BeanResponse updateHotelHtml(@PathVariable Integer id,
                                        @RequestBody Map<String, Object> requestBody) {
        Integer code = (Integer) requestBody.get("code");
        String html = (String) requestBody.get("html");
        if (!StringUtils.hasText(html)){
            return BeanResponse.badRequest("请输入文本");
        }
        Hotel hotel = hotelService.updateHotelHtml(code, id, html);
        if (hotel != null) {
            return BeanResponse.ok(hotel, "修改成功");
        } else {
            return BeanResponse.badRequest("修改失败");
        }
    }

    @PutMapping("room")
    public BeanResponse updateRoom(@RequestBody Room room) {
        if (hotelService.updateRoom(room)) {
            return BeanResponse.ok(room, "修改成功");
        } else {
            return BeanResponse.badRequest("修改失败");
        }
    }

    @GetMapping("theme/{themeId}/page/{pageNum}")
    public BeanResponse queryAllHotelsByTheme(@PathVariable Integer themeId,
                                              @PathVariable Integer pageNum,
                                              @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        return BeanResponse.ok(hotelService.queryAllHotelByThemeId(themeId, pageNum, pageSize));
    }

    @GetMapping("theme")
    public BeanResponse queryAllHotelTheme() {
        return BeanResponse.ok(hotelService.queryAllHotelTheme());
    }

    @GetMapping("breakfast")
    public BeanResponse queryBedAndBreakfast() {
        return BeanResponse.ok(hotelService.queryBedAndBreakfast());
    }

    @GetMapping("{id}")
    public BeanResponse queryHotelById(@PathVariable Integer id) {
        return BeanResponse.ok(hotelService.queryHotelById(id));
    }

    @GetMapping("{id}/room")
    public BeanResponse queryRoomByHotel(@PathVariable Integer id) {
        return BeanResponse.ok(hotelService.queryRoomByHotel(id));
    }
}
